package com.company.algo.listnode;

import com.company.algo.pojo.ListNode;

public class IntersectionNode {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode p1 = headA;
        ListNode p2 = headB;
        while(p1!=p2){
            if(p1 == null) p1 = headB;
            else p1 = p1.next;
            if(p2 == null) p2 = headA;
            else p2 = p2.next;
        }
        return p1;
    }

    public static void main(String[] args) {
        ListNode headA = new ListNode(1);
        ListNode p = headA;
        for (int i = 0; i < 3; i++) {
            p.next = new ListNode(i+2);
            p = p.next;
        }
        ListNode headB = new ListNode(5);
        headB.next = new ListNode(6);

        IntersectionNode Main = new IntersectionNode();
        System.out.println(Main.getIntersectionNode(headA, headB));
    }
}
